﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Text;
using System.Threading.Tasks;
using Dapper;
using BCCommon;
using BCCommon.Extensions;
using BCCommon.OAEnums.Workflow;
using BCEntity.OA.Workflow.WorkflowActivityInstance;
using BCEntity.OA.Workflow.WorkflowActivityInstanceReference;
using BCEntity.OA.Workflow.WorkflowActivityStepExtendMethodInstance;
using BCEntity.OA.Workflow.WorkflowActivityStepInstance;

namespace BCData.OA.Workflow.WorkflowActivityInstanceBusiness
{
    public class WorkflowActivityInstanceBusinessData : IWorkflowActivityInstanceBusinessData
    {
        private const string QUERY = @"SELECT WorkflowActivityInstanceId,WorkflowActivityInstanceNo,WorkflowActivityId,WorkflowKey,WorkflowType,WorkflowName,CompanyId,
                                              CreateTime,TotalStep,CurrentlyStep,ActivityStatus,IsTemplateActivity,AuthorAdminId,AuthorFullName,IsBaseWorkflow,PartnerId,
                                              RoleType,OnBefore,OnAfter
                                       FROM OA_WorkflowActivityInstance";
        private const string UPDATE = @"UPDATE `oa_workflowactivityinstance`
SET
    `WorkflowActivityInstanceNo` = ?WorkflowActivityInstanceNo,
    `WorkflowActivityId` = ?WorkflowActivityId,
    `WorkflowKey` = ?WorkflowKey,
    `WorkflowType` = ?WorkflowType,
    `WorkflowName` = ?WorkflowName,
    `CompanyId` = ?CompanyId,
    `TotalStep` = ?TotalStep,
    `CurrentlyStep` = ?CurrentlyStep,
    `ActivityStatus` = ?ActivityStatus,
    `IsTemplateActivity` = ?IsTemplateActivity,
    `AuthorAdminId` = ?AuthorAdminId,
    `AuthorFullName` = ?AuthorFullName,
    `IsBaseWorkflow` = ?IsBaseWorkflow,
    `PartnerId` = ?PartnerId,
    `RoleType` = ?RoleType,
    `OnBefore` = ?OnBefore,
    `OnAfter` = ?OnAfter
WHERE `WorkflowActivityInstanceId` = ?WorkflowActivityInstanceId";
        private const string QUERY_STEPS = @"SELECT WorkflowActivityStepInstanceId,WorkflowActivityInstanceNo,WorkflowActivityInstanceId,TaskStep,ParentStep,StepName,AssignBased,
                                                    AssignMode,IsBaseHandleMode,TaskMethodTitle,TaskServiceName,TaskMethodName,TaskMethodParameters,StepType,GotoStep,CompanyId,
                                                    DepartmentId,WorkingGroupId,Role,AdminId,Position,RoleType,CanReject,RejectGotoStep,CanClose,CreateTime,UpdateTime
                                        FROM OA_WorkflowActivityStepInstance";
        private const string QUERY_STEPS_EXTEND_METHOD = @"SELECT WorkflowActivityStepExtendMethodInstanceId,WorkflowActivityInstanceNo,WorkflowActivityInstanceId,TaskStep,ExtendMethodName,ExtendMethodCode,
                                            ExtendMethodTitle,TaskServiceName,TaskMethodName,TaskMethodParameters,IsCustom,GotoStep
                                        FROM OA_WorkflowActivityStepExtendMethodInstance";

        private readonly IDatabaseContext databaseContext;

        public WorkflowActivityInstanceBusinessData(IDatabaseContext databaseContext)
        {
            this.databaseContext=databaseContext;
        }

        /// <summary>
        /// 获取添加活动实例脚本
        /// </summary>
        /// <param name="workflowActivityInstanceEntity">工作流活动实例</param>
        /// <param name="dynamicParameters">参数列表</param>
        /// <returns></returns>
        private string GetInsertActivityInstanceSql(WorkflowActivityInstanceEntity workflowActivityInstanceEntity, ref List<DynamicParameters> dynamicParameters)
        {
            string sql = SqlCommands.GetInsertSql("OA_WorkflowActivityInstance", new string[] { "WorkflowActivityInstanceNo", "WorkflowActivityId", "WorkflowKey", "WorkflowType",
                    "WorkflowName","CompanyId","TotalStep","CurrentlyStep","IsTemplateActivity","AuthorAdminId","AuthorFullName","IsBaseWorkflow","RoleType","OnBefore","OnAfter" });

            DynamicParameters parameters = new DynamicParameters();

            parameters.Add("?WorkflowActivityInstanceNo", workflowActivityInstanceEntity.WorkflowActivityInstanceNo, DbType.AnsiString, size: 50);
            parameters.Add("?WorkflowActivityId", workflowActivityInstanceEntity.WorkflowActivityId, DbType.Int64, size: 20);
            parameters.Add("?WorkflowKey", workflowActivityInstanceEntity.WorkflowKey, DbType.AnsiString, size: 150);
            parameters.Add("?WorkflowType", workflowActivityInstanceEntity.WorkflowType, DbType.AnsiString, size: 150);
            parameters.Add("?WorkflowName", workflowActivityInstanceEntity.WorkflowName, DbType.AnsiString, size: 50);
            parameters.Add("?CompanyId", workflowActivityInstanceEntity.CompanyId, DbType.Int32, size: 10);
            parameters.Add("?TotalStep", workflowActivityInstanceEntity.TotalStep, DbType.Int32, size: 10);
            parameters.Add("?CurrentlyStep", workflowActivityInstanceEntity.CurrentlyStep, DbType.Int32, size: 10);
            parameters.Add("?IsTemplateActivity", workflowActivityInstanceEntity.IsTemplateActivity, DbType.Boolean);
            parameters.Add("?AuthorAdminId", workflowActivityInstanceEntity.AuthorAdminId, DbType.Int64, size: 20);
            parameters.Add("?AuthorFullName", workflowActivityInstanceEntity.AuthorFullName, DbType.AnsiString, size: 20);
            parameters.Add("?IsBaseWorkflow", workflowActivityInstanceEntity.IsBaseWorkflow, DbType.Boolean);
            parameters.Add("?RoleType", workflowActivityInstanceEntity.RoleType.ToString(), DbType.AnsiString);
            parameters.Add("?OnBefore", workflowActivityInstanceEntity.OnBefore, DbType.AnsiString, size: 200);
            parameters.Add("?OnAfter", workflowActivityInstanceEntity.OnAfter, DbType.AnsiString, size: 200);

            dynamicParameters.Add(parameters);

            return sql;
        }

        /// <summary>
        /// 获取添加活动步骤实例脚本
        /// </summary>
        /// <param name="workflowActivityStepInstanceEntities">活动步骤实例列表</param>
        /// <param name="dynamicParameters">参数列表</param>
        /// <returns></returns>
        private string[] GetInsertActivityStepInstanceSql(List<WorkflowActivityStepInstanceEntity> workflowActivityStepInstanceEntities, ref List<DynamicParameters> dynamicParameters)
        {
            List<string> sqls = new List<string>();
            string sql = SqlCommands.GetInsertSql("OA_WorkflowActivityStepInstance", new string[] { "WorkflowActivityInstanceNo","TaskStep","ParentStep","StepName","AssignBased","AssignMode",
                    "IsBaseHandleMode","TaskMethodTitle","TaskServiceName","TaskMethodName","TaskMethodParameters","StepType","GotoStep","CompanyId","DepartmentId","WorkingGroupId","Role","AdminId","Position","RoleType","CanReject","RejectGotoStep","CanClose" });

            foreach (var entity in workflowActivityStepInstanceEntities)
            {
                DynamicParameters parameters = new DynamicParameters();
                parameters.Add("?WorkflowActivityInstanceNo", entity.WorkflowActivityInstanceNo, DbType.AnsiString, size: 50);
                parameters.Add("?TaskStep", entity.TaskStep, DbType.Int32, size: 10);
                parameters.Add("?ParentStep", entity.ParentStep, DbType.Int32, size: 10);
                parameters.Add("?StepName", entity.StepName, DbType.AnsiString, size: 50);
                parameters.Add("?AssignBased", entity.AssignBased.ToString(), DbType.AnsiString);
                parameters.Add("?AssignMode", entity.AssignMode.IsNotNull() ? entity.AssignMode.ToString() : null, DbType.AnsiString);
                parameters.Add("?IsBaseHandleMode", entity.IsBaseHandleMode, DbType.Boolean);
                parameters.Add("?TaskMethodTitle", entity.TaskMethodTitle, DbType.AnsiString, size: 100);
                parameters.Add("?TaskServiceName", entity.TaskServiceName, DbType.AnsiString, size: 200);
                parameters.Add("?TaskMethodName", entity.TaskMethodName, DbType.AnsiString, size: 100);
                parameters.Add("?TaskMethodParameters", entity.TaskMethodParameters, DbType.AnsiString, size: 1000);
                parameters.Add("?StepType", entity.StepType.ToString(), DbType.AnsiString);
                parameters.Add("?GotoStep", entity.GotoStep, DbType.Int32, size: 10);
                parameters.Add("?CompanyId", entity.CompanyId, DbType.Int32);
                parameters.Add("?DepartmentId", entity.DepartmentId, DbType.Int32, size: 10);
                parameters.Add("?WorkingGroupId", entity.WorkingGroupId, DbType.Int64, size: 20);
                parameters.Add("?Role", entity.Role, DbType.AnsiString, size: 150);
                parameters.Add("?AdminId", entity.AdminId, DbType.Int64, size: 20);
                parameters.Add("?Position", entity.Position, DbType.AnsiString, size: 150);
                parameters.Add("?RoleType", entity.RoleType.ToString(), DbType.AnsiString);
                parameters.Add("?CanReject", entity.CanReject, DbType.Boolean);
                parameters.Add("?RejectGotoStep", entity.RejectGotoStep ?? null, DbType.Int32);
                parameters.Add("?CanClose", entity.CanClose, DbType.Boolean);
                sqls.Add(sql);
                dynamicParameters.Add(parameters);
            }
            return sqls.ToArray();
        }

        /// <summary>
        /// 获取添加活动柜步骤扩展方法实例脚本
        /// </summary>
        /// <param name="extendMethodInstanceEntities">扩展方法实例列表</param>
        /// <param name="dynamicParameters">参数列表</param>
        /// <returns></returns>
        private string[] GetInsertActivityStepExtendMethodInstanceSql(List<WorkflowActivityStepExtendMethodInstanceEntity> extendMethodInstanceEntities, ref List<DynamicParameters> dynamicParameters)
        {
            List<string> sqls = new List<string>();
            string sql = SqlCommands.GetInsertSql("OA_WorkflowActivityStepExtendMethodInstance", new string[] { "WorkflowActivityInstanceNo", "TaskStep","ExtendMethodCode",
                        "ExtendMethodName", "ExtendMethodTitle" ,"TaskServiceName" ,"TaskMethodName", "TaskMethodParameters", "IsCustom","GotoStep"});
            foreach (var entity in extendMethodInstanceEntities)
            {
                DynamicParameters parameters = new DynamicParameters();
                parameters.Add("?WorkflowActivityInstanceNo", entity.WorkflowActivityInstanceNo, DbType.AnsiString, size: 50);
                parameters.Add("?TaskStep", entity.TaskStep, DbType.Int32, size: 20);
                parameters.Add("?ExtendMethodCode", entity.ExtendMethodCode, DbType.AnsiString, size: 50);
                parameters.Add("?ExtendMethodName", entity.ExtendMethodName, DbType.AnsiString, size: 50);
                parameters.Add("?ExtendMethodTitle", entity.ExtendMethodTitle, DbType.AnsiString, size: 50);
                parameters.Add("?TaskServiceName", entity.TaskServiceName, DbType.AnsiString, size: 200);
                parameters.Add("?TaskMethodName", entity.TaskMethodName, DbType.AnsiString, size: 100);
                parameters.Add("?TaskMethodParameters", entity.TaskMethodParameters, DbType.AnsiString, size: 1000);
                parameters.Add("?IsCustom", entity.IsCustom, DbType.Boolean);
                parameters.Add("?GotoStep", entity.GotoStep, DbType.Int32, size: 10);
                sqls.Add(sql);
                dynamicParameters.Add(parameters);
            }
            return sqls.ToArray();
        }

        public async Task<bool> Add(WorkflowActivityInstanceEntity workflowActivityInstanceEntity)
        {
            List<string> sqls = new List<string>();
            List<DynamicParameters> parameters = new List<DynamicParameters>();
            sqls.Add(GetInsertActivityInstanceSql(workflowActivityInstanceEntity, ref parameters));
            sqls.AddRange(GetInsertActivityStepInstanceSql(workflowActivityInstanceEntity.Steps, ref parameters));
            if (workflowActivityInstanceEntity.ExtendMethods != null && workflowActivityInstanceEntity.ExtendMethods.Count > 0)
            {
                sqls.AddRange(GetInsertActivityStepExtendMethodInstanceSql(workflowActivityInstanceEntity.ExtendMethods, ref parameters));
            }
            return await this.databaseContext.Master.TransactionExecuteAsync(sqls, parameters, databaseContext.Transaction) > 0;
        }

        public async Task<(IEnumerable<WorkflowActivityStepInstanceEntity>, WorkflowActivityInstanceEntity)> GetInstanceInfo(long workflowActivityInstanceId)
        {
            string sql = string.Format(@"{0} WHERE WorkflowActivityInstanceId=?WorkflowActivityInstanceId", QUERY_STEPS);
            sql = string.Format("{0};{1} WHERE WorkflowActivityInstanceId=?WorkflowActivityInstanceId", sql, QUERY);
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?WorkflowActivityInstanceId", workflowActivityInstanceId, DbType.Int64, size: 20);
            return await this.databaseContext.Slave.QueryMultipleAsync<WorkflowActivityStepInstanceEntity, WorkflowActivityInstanceEntity>(sql, parameters);
        }

        public async Task<long> GetId(string workflowActivityInstanceNo)
        {
            string sql = @"SELECT WorkflowActivityInstanceId FROM OA_WorkflowActivityInstance WHERE WorkflowActivityInstanceNo=?WorkflowActivityInstanceNo";
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?WorkflowActivityInstanceNo", workflowActivityInstanceNo, DbType.AnsiString, size: 150);
            return await this.databaseContext.Slave.ExecuteScalarAsync<long>(sql, parameters);
        }

        public async Task<bool> UpdateWorkflowActivityInstanceId(string workflowActivityInstanceNo, long workflowActivityInstanceId)
        {
            string sql = @"UPDATE OA_WorkflowActivityStepInstance SET WorkflowActivityInstanceId=?WorkflowActivityInstanceId WHERE WorkflowActivityInstanceNo=?WorkflowActivityInstanceNo;";
            sql = string.Format(@"{0} UPDATE OA_WorkflowActivityStepExtendMethodInstance SET WorkflowActivityInstanceId=?WorkflowActivityInstanceId WHERE WorkflowActivityInstanceNo=?WorkflowActivityInstanceNo;", sql);
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?WorkflowActivityInstanceNo", workflowActivityInstanceNo, DbType.AnsiString, size: 150);
            parameters.Add("?WorkflowActivityInstanceId", workflowActivityInstanceId, DbType.Int64, size: 20);
            return await this.databaseContext.Master.ExecuteAsync(sql, parameters, databaseContext.Transaction) > 0;
        }

        public async Task<bool> UpdateWorkflowActivityCurrentlyStepNext(long workflowActivityInstanceId)
        {
            string sql = @"UPDATE OA_WorkflowActivityInstance SET CurrentlyStep=CurrentlyStep+1 WHERE WorkflowActivityInstanceId=?WorkflowActivityInstanceId";
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?WorkflowActivityInstanceId", workflowActivityInstanceId, DbType.Int64, size: 20);
            return await this.databaseContext.Master.ExecuteAsync(sql, parameters, databaseContext.Transaction) > 0;
        }

        public async Task<bool> UpdateWorkflowActivityCurrentlyStepPrevious(long workflowActivityInstanceId)
        {
            string sql = @"UPDATE OA_WorkflowActivityInstance SET CurrentlyStep=CurrentlyStep-1 WHERE WorkflowActivityInstanceId=?WorkflowActivityInstanceId";
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?WorkflowActivityInstanceId", workflowActivityInstanceId, DbType.Int64, size: 20);
            return await this.databaseContext.Master.ExecuteAsync(sql, parameters, databaseContext.Transaction) > 0;
        }
        /// <summary>
        /// 返回到设定的步骤
        /// </summary>
        /// <param name="workflowActivityInstanceId"></param>
        /// <param name="currentlyStep"></param>
        /// <returns></returns>
        public async Task<bool> UpdateWorkflowActivityCurrentlyStepPrevious(long workflowActivityInstanceId, int currentlyStep)
        {
            string sql = @"UPDATE OA_WorkflowActivityInstance SET CurrentlyStep=?currentlyStep WHERE WorkflowActivityInstanceId=?WorkflowActivityInstanceId";
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?WorkflowActivityInstanceId", workflowActivityInstanceId, DbType.Int64, size: 20);
            parameters.Add("?currentlyStep", currentlyStep, DbType.Int64, size: 20);
            return await this.databaseContext.Master.ExecuteAsync(sql, parameters, databaseContext.Transaction) > 0;
        }

        public async Task<IEnumerable<WorkflowActivityInstanceEntity>> GetReadyActivity()
        {
            string sql = string.Format("{0} WHERE ActivityStatus='Ready'", QUERY);
            return await this.databaseContext.Slave.QueryAsync<WorkflowActivityInstanceEntity>(sql);
        }

        public async Task<IEnumerable<WorkflowActivityStepInstanceEntity>> GetSteps(long workflowActivityInstanceId)
        {
            string sql = string.Format(@"{0} WHERE WorkflowActivityInstanceId=?WorkflowActivityInstanceId", QUERY_STEPS);
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?WorkflowActivityInstanceId", workflowActivityInstanceId, DbType.Int64, size: 20);
            return await this.databaseContext.Slave.QueryAsync<WorkflowActivityStepInstanceEntity>(sql, parameters);
        }

        public async Task<WorkflowActivityInstanceEntity> GetEntity(long workflowActivityInstanceId)
        {
            string sql = string.Format(@"{0} WHERE WorkflowActivityInstanceId=?WorkflowActivityInstanceId", QUERY);
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?WorkflowActivityInstanceId", workflowActivityInstanceId, DbType.Int64, size: 20);
            return await this.databaseContext.Slave.QueryFirstOrDefaultAsync<WorkflowActivityInstanceEntity>(sql, parameters);
        }

        public async Task<IEnumerable<WorkflowActivityStepExtendMethodInstanceEntity>> GetExtendMethods(long workflowActivityInstanceId)
        {
            string sql = string.Format(@"{0} WHERE WorkflowActivityInstanceId=?WorkflowActivityInstanceId", QUERY_STEPS_EXTEND_METHOD);
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?WorkflowActivityInstanceId", workflowActivityInstanceId, DbType.Int64, size: 20);
            return await this.databaseContext.Slave.QueryAsync<WorkflowActivityStepExtendMethodInstanceEntity>(sql, parameters);
        }

        /// <summary>
        /// 根据活动Id获取活动实例信息
        /// </summary>
        /// <param name="workflowActivityId">活动Id</param>
        /// <returns></returns>
        public async Task<WorkflowActivityInstanceEntity> GetByWorkflowActivityId(long workflowActivityId)
        {
            DynamicParameters parameters = new DynamicParameters();
            string sql = string.Format("{0} WHERE WorkflowActivityId=?WorkflowActivityId", QUERY);
            parameters.Add("?WorkflowActivityId", workflowActivityId, DbType.Int64, size: 20);
            return await this.databaseContext.Slave.QueryFirstOrDefaultAsync<WorkflowActivityInstanceEntity>(sql, parameters);
        }

        /// <summary>
        /// 更新工作流实例状态信息
        /// </summary>
        /// <param name="workflowActivityInstanceId">工作流活动实例Id</param>
        /// <param name="status">状态</param>
        /// <returns></returns>
        public async Task<WorkflowActivityInstanceEntity> UpdateActivityStatus(long workflowActivityInstanceId, ActivityInstanceStatus status)
        {
            string sql =SqlCommands.GetUpdateSql("OA_WorkflowActivityInstance", new string[] { "ActivityStatus" }, new string[] { "WorkflowActivityInstanceId" });
            DynamicParameters parameters = new DynamicParameters();
            string querySql = string.Format("{0} {1} WHERE WorkflowActivityInstanceId=?WorkflowActivityInstanceId", sql, QUERY);
            parameters.Add("?WorkflowActivityInstanceId", workflowActivityInstanceId, DbType.Int64);
            parameters.Add("?ActivityStatus", status.ToString(), DbType.AnsiString);
            return await this.databaseContext.Master.QueryFirstOrDefaultAsync<WorkflowActivityInstanceEntity>(querySql, parameters, databaseContext.Transaction);
        }

        public async Task<WorkflowActivityInstanceReferenceEntity> GetActivityInstanceReferenceEntity(long workflowActivityInstanceId)
        {
            string sql = @"SELECT WorkflowActivityInstanceReferenceNo,WorkflowActivityInstanceId,WorkflowType,BusinessType,ReferenceNo,ReferenceType 
                            FROM OA_WorkflowActivityInstanceReference 
                            WHERE WorkflowActivityInstanceId=?WorkflowActivityInstanceId";
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?WorkflowActivityInstanceId", workflowActivityInstanceId, DbType.Int64, size: 20);
            return await this.databaseContext.Slave.QueryFirstOrDefaultAsync<WorkflowActivityInstanceReferenceEntity>(sql, parameters);
        }

        public async Task<bool> UpdateWorkflowActivityCurrentlyStepFirst(long workflowActivityInstanceId)
        {
            string sql = @"UPDATE OA_WorkflowActivityInstance SET CurrentlyStep= 0 WHERE WorkflowActivityInstanceId=?WorkflowActivityInstanceId";
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?WorkflowActivityInstanceId", workflowActivityInstanceId, DbType.Int64, size: 20);
            return await this.databaseContext.Master.ExecuteAsync(sql, parameters, databaseContext.Transaction) > 0;
        }

        public async Task UpdateWorkflowActivityAsync(WorkflowActivityInstanceEntity workflowActivityInstance)
        {
            DynamicParameters parameters = new DynamicParameters();

            parameters.Add("?WorkflowActivityInstanceNo", workflowActivityInstance.WorkflowActivityInstanceNo, DbType.AnsiString, size: 50);
            parameters.Add("?WorkflowActivityId", workflowActivityInstance.WorkflowActivityId, DbType.Int64, size: 20);
            parameters.Add("?WorkflowKey", workflowActivityInstance.WorkflowKey, DbType.AnsiString, size: 150);
            parameters.Add("?WorkflowType", workflowActivityInstance.WorkflowType, DbType.AnsiString, size: 150);
            parameters.Add("?WorkflowName", workflowActivityInstance.WorkflowName, DbType.AnsiString, size: 50);
            parameters.Add("?CompanyId", workflowActivityInstance.CompanyId, DbType.Int32, size: 10);
            parameters.Add("?TotalStep", workflowActivityInstance.TotalStep, DbType.Int32, size: 10);
            parameters.Add("?CurrentlyStep", workflowActivityInstance.CurrentlyStep, DbType.Int32, size: 10);
            parameters.Add("?ActivityStatus", workflowActivityInstance.ActivityStatus.ToString(), DbType.AnsiString);
            parameters.Add("?IsTemplateActivity", workflowActivityInstance.IsTemplateActivity, DbType.Boolean);
            parameters.Add("?AuthorAdminId", workflowActivityInstance.AuthorAdminId, DbType.Int64, size: 20);
            parameters.Add("?AuthorFullName", workflowActivityInstance.AuthorFullName, DbType.AnsiString, size: 20);
            parameters.Add("?IsBaseWorkflow", workflowActivityInstance.IsBaseWorkflow, DbType.Boolean);
            parameters.Add("?PartnerId", workflowActivityInstance.PartnerId, DbType.Int32, size: 10);
            parameters.Add("?RoleType", workflowActivityInstance.RoleType.ToString(), DbType.AnsiString);
            parameters.Add("?OnBefore", workflowActivityInstance.OnBefore, DbType.AnsiString, size: 200);
            parameters.Add("?OnAfter", workflowActivityInstance.OnAfter, DbType.AnsiString, size: 200);
            parameters.Add("?WorkflowActivityInstanceId", workflowActivityInstance.WorkflowActivityInstanceId, DbType.Int64, size: 20);

            await this.databaseContext.Master.ExecuteAsync(UPDATE, parameters, databaseContext.Transaction);
        }

        public async Task<int> UpdateActivityStatus(List<long> workflowActivityInstanceIdList, ActivityInstanceStatus status)
        {
            string sql = "UPDATE OA_WorkflowActivityInstance SET ActivityStatus = ?ActivityStatus WHERE FIND_IN_SET(WorkflowActivityInstanceId,?WorkflowActivityInstanceIds)";
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?WorkflowActivityInstanceIds", string.Join(",", workflowActivityInstanceIdList), DbType.AnsiString);
            parameters.Add("?ActivityStatus", status.ToString(), DbType.AnsiString);
            return await this.databaseContext.Master.ExecuteAsync(sql, parameters, this.databaseContext.Transaction);
        }
    }
}
